package com.hw.paltfrom.service.system;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.mapper.HwBaseAreaMapper;
import com.hw.paltfrom.mapper.HwBaseAuthorizeMapper;
import com.hw.paltfrom.mapper.HwBaseEnterpriseMapper;
import com.hw.paltfrom.mapper.HwBaseModuleMapper;
import com.hw.paltfrom.mapper.HwBaseOffsiteMapper;
import com.hw.paltfrom.mapper.HwBaseOverrunstationMapper;
import com.hw.paltfrom.mapper.HwBaseUserRelationMapper;
import com.hw.paltfrom.pojo.HwBaseArea;
import com.hw.paltfrom.pojo.HwBaseAuthorize;
import com.hw.paltfrom.pojo.HwBaseEnterprise;
import com.hw.paltfrom.pojo.HwBaseModule;
import com.hw.paltfrom.pojo.HwBaseOffsite;
import com.hw.paltfrom.pojo.HwBaseOverrunstation;
import com.hw.paltfrom.pojo.HwBaseUserRelation;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BaseUserVo;
import com.hw.paltfrom.vo.SortListVo;

@Service
public class SystemServer {
	@Autowired
	private HwBaseAreaMapper hwBaseAreaMapper;

	@Autowired
	private HwBaseEnterpriseMapper baseEnterpriseMapper;

	@Autowired
	private HwBaseOverrunstationMapper overrunstationMapper;

	@Autowired
	private HwBaseOffsiteMapper offsiteMapper;

	// @Autowired
	// private HwBaseRoleMapper baseRoleMapper;

	@Autowired
	private HwBaseUserRelationMapper baseUserRelationMapper;

	@Autowired
	private HwBaseAuthorizeMapper hwBaseAuthorizeMapper;

	@Autowired
	private HwBaseModuleMapper baseModuleMapper;

	public Map<String, Object> indexList(BaseUserVo vo) {

		String getfUserid = vo.getfUserid();
		LinkedHashMap<String, LinkedHashMap<String, String>> allList = new LinkedHashMap<String, LinkedHashMap<String, String>>();

		// 查询该用户的所有角色
		EntityWrapper<HwBaseUserRelation> burWrapper = new EntityWrapper<HwBaseUserRelation>();
		burWrapper.eq("F_UserId", getfUserid);
		List<HwBaseUserRelation> burList = baseUserRelationMapper.selectList(burWrapper);

		// 存当前用户的所有能看的页面id
		List<String> objectList = new ArrayList<String>();
		for (HwBaseUserRelation bur : burList) {
			// 查询每个角色有那些页面权限
			EntityWrapper<HwBaseAuthorize> entityWrapper = new EntityWrapper<HwBaseAuthorize>();
			entityWrapper.eq("F_RoleId", bur.getfRoleid());
			List<HwBaseAuthorize> selectList = hwBaseAuthorizeMapper.selectList(entityWrapper);
			for (HwBaseAuthorize hba : selectList) {
				objectList.add(hba.getfObjectid());
			}
		}

		// 将页面id 分配层级

		// 父类排序
		EntityWrapper<HwBaseModule> parentWrapper = new EntityWrapper<HwBaseModule>();
		parentWrapper.in("F_ModuleId", objectList).and().eq("F_ParentId", "0").orderBy("F_SortCode");
		List<HwBaseModule> parentList = baseModuleMapper.selectList(parentWrapper);

		for (HwBaseModule a : parentList) {
//			System.out.println(a.getfFullname() + "++" + a.getfSortcode());
		}

		// 子类排序
		EntityWrapper<HwBaseModule> sonWrapper = new EntityWrapper<HwBaseModule>();
		sonWrapper.in("F_ModuleId", objectList).and().ne("F_ParentId", "0").orderBy("F_SortCode");
		List<HwBaseModule> sonList  = baseModuleMapper.selectList(sonWrapper);
		// for(HwBaseModule str:parentList){
		// System.out.println(str.getfFullname());
		// }
	
		
		
		for (HwBaseModule son : sonList) {
			String sonParentid = son.getfParentid();

			for (HwBaseModule parent : parentList) {
				String Parentid = parent.getfModuleid();
				String parentname = parent.getfFullname();
				if (sonParentid.equals(Parentid)) {
					LinkedHashMap<String, String> oldMap = allList.get(parentname);
					if (oldMap == null) {
						LinkedHashMap<String, String> newmap = new LinkedHashMap<String, String>();
						newmap.put(son.getfFullname(), son.getfModuleid());
						allList.put(parentname, newmap);
					} else {
						oldMap.put(son.getfFullname(), son.getfModuleid());
						allList.put(parentname, oldMap);
					}
				}
			}

		}
		
		
		LinkedHashMap<String, LinkedHashMap<String, String>> result = new LinkedHashMap<String, LinkedHashMap<String, String>>();
		for (HwBaseModule parent : parentList) {
			Set<String> keys = allList.keySet();
//			keys是父级名称
			for(String str:keys){
				if(str.equals(parent.getfFullname())){
					result.put(str, allList.get(str));
				}
			}
			
//			System.out.println(parent.getfFullname());
			
			
		}
		
		

		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, result);
	}

	public Map<String, Object> sortList(SortListVo vo) {

		Integer type = vo.getType();

		String fAeraid = vo.getfAeraid();
		Integer fType = vo.getfType();
		String stationCode = vo.getStationCode();

		Wrapper<HwBaseArea> wrapper = new EntityWrapper<HwBaseArea>();

		// LinkedHashMap<String, List<String>> result = new
		// LinkedHashMap<String, List<String>>();
		LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
		// LinkedHashMap<String, LinkedHashMap<String,String>> result = new
		// LinkedHashMap<String, LinkedHashMap<String,String>>();

		ArrayList<LinkedHashMap<String, Object>> resultList = new ArrayList<LinkedHashMap<String, Object>>();
		if (fType == 1) {
			// 查全市
			wrapper.isNotNull("F_ParentId");
		}
		if (fType == 2||fType==3||fType==4||fType==5||fType==6) {
			// 查单个县
			if(StringUtils.isNotBlank(fAeraid)){
				wrapper.eq("F_AreaId", fAeraid);
			}
		}

		wrapper.eq("F_DeleteMark", "0");
		// 拿到所有县编码
		wrapper.orderBy("F_SortCode");
		List<HwBaseArea> city = hwBaseAreaMapper.selectList(wrapper);
		for (HwBaseArea a : city) {

			// 存该县下的下极
			ArrayList<String> nameList = new ArrayList<>();

			// 存该县下的下极集合
			ArrayList<Object> childrenList = new ArrayList<Object>();
			
			// 存该县下的名字及编码
			LinkedHashMap<String,Object> cityMap = new LinkedHashMap<>();
			String getfAreacode = a.getfAreacode();

			// 查源头企业
			if (type == 0) {
				
				Wrapper<HwBaseEnterprise> EnterpriseWrapper = new EntityWrapper<HwBaseEnterprise>();
				EnterpriseWrapper.eq("F_AreaId", getfAreacode).eq("F_DeleteMark", "0").eq("F_EnabledMark", "1");
				List<HwBaseEnterprise> selectList = baseEnterpriseMapper.selectList(EnterpriseWrapper);
//				查该县下所有的源头企业
				for (HwBaseEnterprise he : selectList) {
					// 存该县下的下极
					LinkedHashMap<String,Object> childrenMap = new LinkedHashMap<>();
					 childrenMap.put("id", he.getfEnterprisecode());
					 childrenMap.put("title",he.getfEnterprisename());
					 childrenList.add(childrenMap);
				}
				cityMap.put("title", a.getfAreaname());
				cityMap.put("id", a.getfAreaid());
				cityMap.put("children", childrenList);
				resultList.add(cityMap);
			}

			// 查超限站
			if (type == 1) {
				Wrapper<HwBaseOverrunstation> OverrunWrapper = new EntityWrapper<HwBaseOverrunstation>();
				if(StringUtils.isNotBlank(stationCode)){
					OverrunWrapper.eq("F_OverrunCode", stationCode);
				}
				if(StringUtils.isNotBlank(getfAreacode)){
					OverrunWrapper.eq("F_AreaId", getfAreacode);
				}
				OverrunWrapper.eq("F_DeleteMark", "0").eq("F_EnabledMark", "1");
				List<HwBaseOverrunstation> selectList = overrunstationMapper.selectList(OverrunWrapper);
				for (HwBaseOverrunstation he : selectList) {
					LinkedHashMap<String,Object> childrenMap = new LinkedHashMap<>();
					 childrenMap.put("id", he.getfOverruncode());
					 childrenMap.put("title",he.getfOverrunname());
					 childrenList.add(childrenMap);
				}
				if(fType==3){
					if(!childrenList.isEmpty()){
						cityMap.put("title", a.getfAreaname());
						cityMap.put("id", a.getfAreaid());
						cityMap.put("children", childrenList);
						resultList.add(cityMap);
					}
					
				}else{
					cityMap.put("title", a.getfAreaname());
					cityMap.put("id", a.getfAreaid());
					cityMap.put("children", childrenList);
					resultList.add(cityMap);
				}
			}

			// 查非现场检测点
			if (type == 2) {
				Wrapper<HwBaseOffsite> offSiteWrapper = new EntityWrapper<HwBaseOffsite>();
				offSiteWrapper.eq("F_AreaId", getfAreacode).eq("F_DeleteMark", "0").eq("F_EnabledMark", "1");
				List<HwBaseOffsite> selectList = offsiteMapper.selectList(offSiteWrapper);
				for (HwBaseOffsite he : selectList) {
					LinkedHashMap<String,Object> childrenMap = new LinkedHashMap<>();
					 childrenMap.put("id", he.getfOffsitecode());
					 childrenMap.put("title",he.getfOffsitename());
					 childrenList.add(childrenMap);
				}

				cityMap.put("title", a.getfAreaname());
				cityMap.put("id", a.getfAreaid());
				cityMap.put("children", childrenList);
				resultList.add(cityMap);
			}

			// 只查市县结构
			if (type == 3) {
				result.put(a.getfAreaname(), a.getfAreacode());
			}

			// 查县下的用户
		}
		if(type==3){
			return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, result);
		}
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, resultList);
	}

	
	public Map<String, Object> mainList(BaseUserVo vo) {

		String getfUserid = vo.getfUserid();
		LinkedHashMap<String,Object> allList = new LinkedHashMap<String, Object>();

		// 查询该用户的所有角色
		EntityWrapper<HwBaseUserRelation> burWrapper = new EntityWrapper<HwBaseUserRelation>();
		burWrapper.eq("F_UserId", getfUserid);
		List<HwBaseUserRelation> burList = baseUserRelationMapper.selectList(burWrapper);

		// 存当前用户的所有能看的页面id
		List<String> objectList = new ArrayList<String>();
		for (HwBaseUserRelation bur : burList) {
			// 查询每个角色有那些页面权限
			EntityWrapper<HwBaseAuthorize> entityWrapper = new EntityWrapper<HwBaseAuthorize>();
			entityWrapper.eq("F_RoleId", bur.getfRoleid());
			List<HwBaseAuthorize> selectList = hwBaseAuthorizeMapper.selectList(entityWrapper);
			for (HwBaseAuthorize hba : selectList) {
				objectList.add(hba.getfObjectid());
			}
		}

		// 将页面id 分配层级

		// 父类排序
		EntityWrapper<HwBaseModule> parentWrapper = new EntityWrapper<HwBaseModule>();
		parentWrapper.in("F_ModuleId", objectList).and().eq("F_ParentId", "0").orderBy("F_SortCode");
		List<HwBaseModule> parentList = baseModuleMapper.selectList(parentWrapper);

		for (HwBaseModule a : parentList) {
//			System.out.println(a.getfFullname() + "++" + a.getfSortcode());
		}

		// 子类排序
		EntityWrapper<HwBaseModule> sonWrapper = new EntityWrapper<HwBaseModule>();
		sonWrapper.in("F_ModuleId", objectList).and().ne("F_ParentId", "0").orderBy("F_SortCode");
		List<HwBaseModule> sonList  = baseModuleMapper.selectList(sonWrapper);
		// for(HwBaseModule str:parentList){
		// System.out.println(str.getfFullname());
		// }
	
		
		
		for (HwBaseModule son : sonList) {
			String sonParentid = son.getfParentid();

			for (HwBaseModule parent : parentList) {
				String Parentid = parent.getfModuleid();
				String parentname = parent.getfFullname();
				if (sonParentid.equals(Parentid)) {
					ArrayList<Object>  oldMap =  (ArrayList<Object>) allList.get(parentname);
					if (oldMap == null) {
						ArrayList<Object> newChildrenList = new ArrayList<>();
						LinkedHashMap<String, String> childremData = new LinkedHashMap<String, String>();
						childremData.put("title", son.getfFullname());
						childremData.put("id", son.getfModuleid());
						childremData.put("path", son.getfUrladdress());
						newChildrenList.add(childremData);
						allList.put(parentname, newChildrenList);
					} else {
						ArrayList<Object> newChildrenList = new ArrayList<>();
						LinkedHashMap<String, String> childremData = new LinkedHashMap<String, String>();
						childremData.put("title", son.getfFullname());
						childremData.put("id", son.getfModuleid());
						childremData.put("path", son.getfUrladdress());
						newChildrenList.add(childremData);
						
						oldMap.addAll(newChildrenList);
						allList.put(parentname, oldMap);
					}
				}
			}

		}
		
		
		LinkedHashMap<String, LinkedHashMap<String, String>> result = new LinkedHashMap<String, LinkedHashMap<String, String>>();
		LinkedHashMap<String,Object> resultList = new LinkedHashMap<String, Object>();
		
//		for (HwBaseModule parent : parentList) {
//			Set<String> keys = allList.keySet();
////			keys是父级名称
//			for(String str:keys){
//				if(str.equals(parent.getfFullname())){
//					result.put(str, allList.get(str));
//				}
//			}
//			
////			System.out.println(parent.getfFullname());
//			
//			
//		}
//		LinkedHashMap<String,Object> allList = new LinkedHashMap<String, Object>();
		for (HwBaseModule parent : parentList) {
			Set<String> keys = allList.keySet();
//			keys是父级名称
			for(String str:keys){
				if(str.equals(parent.getfFullname())){
					resultList.put(str, allList.get(str));
				}
			}
			
//			System.out.println(parent.getfFullname());
			
			
		}
		

		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, resultList);
	}
	
}
